package com.common;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

/**
 * Created by 10742 on 2017/7/6.
 */
public class WaterQualityDetectionTool extends JdbcDaoSupport{

    public int insert(final int indexNumber, final String indexName, final String indexValue, final String ID_WaterLayer){
        final String sql = "insert into WaterQuality_SurStatMonData(Index_id, Index_Name, Index_Value, WaterLayer_id) values(?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setInt(1, indexNumber);
                ps.setString(2, indexName);
                ps.setString(3, indexValue);
                ps.setString(4, ID_WaterLayer);
                return ps;
            }
        }, keyHolder);
        return keyHolder.getKey().intValue();
    }

    public int update(int detectionID, int indexNumber, String indexName, String indexValue){
        String sql = "update WaterQuality_SurStatMonData set Index_id=?,Index_Name=?,Index_Value=? where ID=?";
        return getJdbcTemplate().update(sql, indexNumber, indexName, indexValue, detectionID);
    }

    public int delete(int detectionID){
        String sql = "delete from WaterQuality_SurStatMonData where ID=?";
        return getJdbcTemplate().update(sql, detectionID);
    }

}
